package edu.jxut.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.jxut.reggie.common.R;
import edu.jxut.reggie.dtos.DishDto;
import edu.jxut.reggie.dtos.SetmealDto;
import edu.jxut.reggie.dtos.SetnealDtooo;
import edu.jxut.reggie.pojos.Setmeal;
import edu.jxut.reggie.pojos.SetmealDish;
import edu.jxut.reggie.server.DishService;
import edu.jxut.reggie.server.SetmealService;
import net.sf.jsqlparser.expression.LongValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private DishService dishService;

    @PostMapping
    public R<String> setmealSave(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
        return R.success("新增成功!");
    }

    @GetMapping("page")
    private R<Page> queryBypage(@RequestParam("page")Integer page
            ,@RequestParam("pageSize")Integer pageSize
            ,@RequestParam(value = "name",required = false) String name){

        Page<SetmealDto> setmealDtoPage = setmealService.selectByPage(page, pageSize, name);

        return R.success(setmealDtoPage);
    }

    @GetMapping("{id}")
    public R<SetmealDto> queryOne(@PathVariable("id") Long setmealId){
        SetmealDto setmealDto = setmealService.selectOne(setmealId);
        return R.success(setmealDto);
    }

    @PutMapping
    public R<String> updateSetmeal(@RequestBody SetmealDto setmealDto){
        setmealService.UpdateWithDish(setmealDto);
        return R.success("修改成功");
    }

    @DeleteMapping
    public R<String> deletSetmealWithDish(@RequestParam("ids")String ids){
        String[] split = ids.split("\\,");
        List<Long> collect = Arrays.stream(split).map(s -> Long.valueOf(s)).collect(Collectors.toList());

        setmealService.removeWithDish(collect);
        return R.success("删除成功");
    }

    @PostMapping("/status/{status}")
    public R<String> BatchEdit(@PathVariable("status") Integer status,@RequestParam("ids")String ids){
        String[] split = ids.split("\\,");
        List<Long> collect = Arrays.stream(split).map(s -> Long.valueOf(s)).collect(Collectors.toList());

        setmealService.BulkEditStatus(status,collect);
        return R.success("修改成功！");
    }

    /**
     * 根据条件查询套餐数据
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }

    @GetMapping("dish/{id}")
    public R<List> selectDish(@PathVariable("id") Long id){
        SetmealDto setmealDto = setmealService.selectOne(id);
        //拿到dishId
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        List<Long> list = setmealDishes.stream().map(s -> s.getDishId()).collect(Collectors.toList());

        return R.success(dishService.getByIdWithFlavor(list));
    }

}
